package com.liit.controller.weixin.controller;

import javax.annotation.Resource;

import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.liit.controller.weixin.process.ScanMsgProcess;
import com.liit.entity.IntegralDetailFormMap;
import com.liit.entity.WeixinUserFormMap;
import com.liit.mapper.WeixinUserMapper;
import com.liit.util.Common;
import com.liit.util.DictUtils;
import com.liit.util.IntegralUtils;

/**
 * 
 * @author dingweibo
 * @Title: UserInfoController.java
 * @Description: 微信用户信息修改
 * @date 2016年3月31日
 */
@Controller
@RequestMapping("/weixin/user/")
public class UserInfoController extends WeixinBaseController {

	@Resource
	WeixinUserMapper mapper;

	@RequestMapping("editMyinfo")
	public String edit(Model model) throws Exception {
		model.addAttribute("weixinUser", getWeixinUserInfo());
		model.addAttribute("politicalList", DictUtils.getDictByType("POLITICAL_TYPE"));
		return Common.BACKGROUND_PATH + "/weixin/user/editMyinfo";
	}

	@Transactional(readOnly=false)
	@RequestMapping("saveMyinfo")
	public String save(Model model, RedirectAttributes redirectAttributes) throws Exception {
		WeixinUserFormMap weixinUser = getFormMap(WeixinUserFormMap.class);
		Common.nullValueMap(weixinUser);
		mapper.editEntity(weixinUser);
		weixinUser = mapper.findbyFrist("openid", getOpenid(), WeixinUserFormMap.class);
		IntegralDetailFormMap where = new IntegralDetailFormMap();
		where.put("openid", weixinUser.getStr("openid"));
		where.put("description", DictUtils.getFirstDictLabel("INTEGRAL_A"));
		if (CollectionUtils.isEmpty(mapper.findByNames(where))) {
			// 完善资料送积分
			IntegralUtils.getInstance().completeUserinfo(weixinUser.getStr("openid"));
		}
		SecurityUtils.getSubject().getSession().setAttribute("wxUserSession", weixinUser);
		
		// 操作成功消息
		String successMsg = null;
		String icon = null;
		String okLink = getContextPath() + "/weixin/user/myinfo.shtml";

		// 扫码完善资料
		String partyId = getPara("partyId");
		String type = getPara("type");
		if (!partyId.isEmpty() && !type.isEmpty()) {
			int retCode = ScanMsgProcess.scanEvent(weixinUser.getStr("openid"), partyId);
			if (retCode == Common.PARTY_MSG_TYPE_ATTEND_SUCCESS) {
				successMsg = "活动签到成功";
			} else if (retCode == Common.PARTY_MSG_TYPE_SIGN_SUCCESS) {
				successMsg = "活动报名成功";
			} else {
				successMsg = "0".equals(type) ? "活动报名失败" : "1".equals(type) ? "活动签到失败" : null;
				icon = "warn";
			}
			useJSSDK("/weixin/weixinMsg.shtml");
			okLink = "javascript:wx.closeWindow();";
		}

		// 设置msg信息
		return doMsg(icon, successMsg, null, okLink, null, null, null, null, null, redirectAttributes);
	}
}